package com.dyz.wsw.config;

import com.dyz.wsw.domain.market.convert.MarketCardConvert;
import io.r2dbc.spi.ConnectionFactory;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.r2dbc.convert.R2dbcCustomConversions;
import org.springframework.data.r2dbc.dialect.MySqlDialect;
import org.springframework.data.r2dbc.repository.config.EnableR2dbcRepositories;
import org.springframework.r2dbc.connection.R2dbcTransactionManager;
import org.springframework.transaction.ReactiveTransactionManager;
import org.springframework.transaction.annotation.EnableTransactionManagement;
import org.springframework.transaction.reactive.TransactionalOperator;


/**
 * @author dyz
 * Time 2024/6/2 17:50
 */
@Configuration
//启用对R2DBC的支持
//并配置R2DBC的Repositories
//一般当需要配置转换器等操作时，会使用到该注解
//目前没做任何配置，因此该注解暂时没用
@EnableR2dbcRepositories
@EnableTransactionManagement //启动事务管理
@Slf4j
public class R2dbcConfig {

//    @Bean
//    @ConditionalOnMissingBean
    public R2dbcCustomConversions conversions(){
        log.debug("加载r2dbc自定义映射转换器");
        return R2dbcCustomConversions.of(MySqlDialect.INSTANCE,
                new MarketCardConvert()
        );
    }

    @Bean
    public ReactiveTransactionManager transactionManager(ConnectionFactory connectionFactory){
        return new R2dbcTransactionManager(connectionFactory);
    }

    @Bean
    public TransactionalOperator transactionalOperator(ReactiveTransactionManager transactionManager){
        return TransactionalOperator.create(transactionManager);
    }
}
